package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.Question;
import cn.wolfcode.mapper.ExamMapper;
import cn.wolfcode.mapper.QuestionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.jsqlparser.statement.alter.Alter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.*;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private QuestionMapper questionMapper;


    @Override
    public void save(Exam exam) {
        examMapper.insert(exam);
    }

    @Override
    public void delete(Long id) {
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKey(exam);
    }

    @Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Exam> listAll() {
        return examMapper.selectAll();
    }

    @Override
    public PageInfo<Exam> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        //List<Exam> exams = examMapper.selectForList(qo); //里面不需要自己写limit
        // return new PageInfo<Exam>(exams);
        return null;
    }


    @Override
    public  Map<String, List<Question>> saveExam(Exam exam) {

        //插入考试表
        examMapper.insert(exam);
        //根据对象里面的条数从数据获取对应的数据

        //单选题数量
        Integer oneChoiceCount = exam.getOneChoiceCount();
        //发送sql 获取单选题目
        List<Question> oneQuestions = questionMapper.oneChoice(oneChoiceCount);


        //多选题数量
        Integer manyChoiceCount = exam.getManyChoiceCount();
        //获取单选题目
        List<Question> manyQuestions = questionMapper.manyChoiceCount(manyChoiceCount);


        //判断数量
        Integer judgeCount = exam.getJudgeCount();
        List<Question> judgeQuestions = questionMapper.judgeCount(judgeCount);


        //要插入题目总数
        //第一列 试卷id
        Long id = exam.getId();
        //第二列 题目id
        //定义3个集合来存储不同类型的题目id
        List<Long> oneQuestionList = new ArrayList<>();
        List<Long> manyQuestionList = new ArrayList<>();
        List<Long> judgeQuestionList = new ArrayList<>();

        //先遍历获取到对象
        for (Question oneQuestion : oneQuestions) {
            oneQuestionList.add(oneQuestion.getId());
        }
        for (Question manyQuestion : manyQuestions) {
            manyQuestionList.add(manyQuestion.getId());
        }
        for (Question judgeQuestion : judgeQuestions) {
            judgeQuestionList.add(judgeQuestion.getId());
        }
        //排序集合
        Collections.sort(oneQuestionList);
        Collections.sort(manyQuestionList);
        Collections.sort(judgeQuestionList);

        //第四列分数
        //获取单选的分数
        Integer oneChoiceScore = exam.getOneChoiceScore();
        //获取多选的分数
        Integer manyChoiceScore = exam.getManyChoiceScore();
        //获取判断的分数
        Integer judgeScore = exam.getJudgeScore();

        //先插入单选的数据
        for (int i = 0; i < oneQuestionList.size(); i++) {
            examMapper.insertExamQuestionMiddleTable(id, oneQuestionList.get(i), i + 1, oneChoiceScore);
        }

        //插入单多选的数据
        for (int i = 0; i < manyQuestionList.size(); i++) {
            examMapper.insertExamQuestionMiddleTable(id, manyQuestionList.get(i), i + 1, manyChoiceScore);
        }
        //先插入判断的数据
        for (int i = 0; i < judgeQuestionList.size(); i++) {
            examMapper.insertExamQuestionMiddleTable(id, judgeQuestionList.get(i), i + 1, judgeScore);
        }
        Map<String, List<Question>> map = new HashMap<>();
        map.put("oneQuestions" ,oneQuestions);
        map.put("manyQuestions" ,manyQuestions);
        map.put("judgeQuestions" ,judgeQuestions);
        return map;
    }

}